package com.tomcov.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tomcov.client.service.JourneyService;
import com.tomcov.shared.Journey;

public class JourneyServiceImpl extends RemoteServiceServlet implements JourneyService{

	private static final long serialVersionUID = 1L;

	@Override
	public Journey getJounrey(String From, String To) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.Journey.class);
		query.setFilter("From == FromParam");
		query.declareParameters("String FromParam");
		
		try{
			@SuppressWarnings("unchecked")
			List<Journey> results = (List<Journey>) query.execute(From);//To);
			if(results.size()==0){
				return null;
			}
			else{Journey journey= results.get(0);
				return journey;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	@Override
	public Journey AddJourney(Journey journey) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.Journey.class);
		query.setFilter("From == FromParam");
		query.declareParameters("String FromParam");
		try{
			@SuppressWarnings("unchecked")
			List<Journey> results = (List<Journey>) query.execute(journey.getFrom());
			if(results.size()==0)	
				{pm.makePersistent(journey);
				return journey;}
			else{
				return null;
				}
			}
		finally{
			query.closeAll();
			pm.close();
		}
		
	}

}
